<?php
namespace app\api\model;
use think\Model;
use think\model\concern\SoftDelete;
class NursingLang extends Model
{
    use SoftDelete;
    protected $name = "nursing_lang";
    protected $pk = "id";
    protected $deleteTime = "delete_time";
    protected $defaultSoftDelete = 0;
    protected $json = [];
    protected $jsonAssoc = true;
    public function getImgAttr($value){
        if($value){
            $value = getAppFilePath($value);
        }else{
            $value = '';
        }
        return $value;
    }
    public function getBannerAttr($value){
        if($value){
            $value = getAppFilePath($value);
        }else{
            $value = '';
        }
        return $value;
    }
    public function getContentImgsAttr($value){
        if($value){
            $value = getAppFilePath($value,true);
        }else{
            $value = [];
        }
        return $value;
    }
    public function getOrderNum(){
        return $this->hasMany(NursingOrder::class,'nursing_lang_id','id');
    }
    public function getComment(){
        return $this->hasMany(CommentUser::class,'nursing_lang_id','id');
    }
    public function matching($data){
        if(empty($data)){
            return false;
        }
//        echo '<pre>';
//        print_r($data);
//        exit;
        $nursing_lang = $this->where('status',1)
            ->where('type_id',3)
            ->field('id,name')
            ->order('sort desc')
            ->select()->toArray();
        foreach ($data as $keys=>$values){
            $values['ids'] = DiseaseUser::where('option_type_id',$values['id'])
                ->where('name','in',$values['option'])
                ->where('status',1)
                ->column('id');
            $data[$keys] = $values;
        }
        $da = [];
        foreach ($nursing_lang as $key=>$value){
            foreach ($data as $va){
               $option = TermEmergency::where('status',1)
                   ->where('nursing_lang_id',$value['id'])
                   ->where('option_type_id',$va['id'])
                   ->column('disease_user_id');
               $arr = array_diff($va['ids'],$option);
               if(!empty($arr)){
                   break;
               }
            }
            if(!empty($arr)){
                continue;
            }else{
                $da = $value;
                break;
            }
        }

//        echo '<pre>';
//        print_r($da);
//        exit;
        if(empty($da)){
            return false;
        }else{
            return $da;
        }
    }
}